package cn.itripsearch.service.impl;



import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Pages;
import cn.itrip.common.PropertiesUtils;
import cn.itripsearch.beans.vo.ItripHotelVO;
import cn.itripsearch.beans.vo.SearchHotelVO;
import cn.itripsearch.dao.*;
import cn.itripsearch.service.IItripHotelService;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.stereotype.Service;
import java.util.List;


@Service
public class ItripHotelService implements IItripHotelService {
    private static   String URL= PropertiesUtils.get("application.properties","spring.data.solr.host");

    private BaseQuery<ItripHotelVO> hotelVOBaseQuery=new BaseQuery<>(URL);


    @Override
    public Pages<ItripHotelVO> searchItripHotelPage(SearchHotelVO vo, Integer pageNo, Integer pageSize) throws  Exception {
        SolrQuery query = new SolrQuery("*:*");
        StringBuffer tempQuery = new StringBuffer();
        int tempFlag = 0;
        if (EmptyUtils.isNotEmpty(vo)) {
            if (EmptyUtils.isNotEmpty(vo.getDestination())) {
                query.addFilterQuery("redundantProvinceName:" + vo.getDestination() + "*");
                tempFlag = 1;
            }
            if (EmptyUtils.isNotEmpty(vo.getHotelLevel())) {
                query.addFilterQuery("hotelLevel:" + vo.getHotelLevel() + "*");
            }
            if (EmptyUtils.isNotEmpty(vo.getKeywords())) {
                StringBuffer buffer = new StringBuffer();
                if (tempFlag == 1) {
                    buffer.append(" AND keyword :" + vo.getKeywords());
                } else {
                    buffer.append("keyword: " + vo.getKeywords());
                }
                if (EmptyUtils.isNotEmpty(buffer.toString())) {
                    query.setQuery(buffer.toString());
                }
            }
                if (EmptyUtils.isNotEmpty(vo.getFeatureIds())) {
                    StringBuffer buffers = new StringBuffer("(");
                    int flag = 0;
                    String featureIdArray[] = vo.getFeatureIds().split(",");
                    for (String featureId : featureIdArray) {
                        if (flag == 0) {
                            buffers.append("featureIds:" + "*," + featureId + ",*");
                        } else {
                            buffers.append(" OR featureIds:" + "*," + featureId + ",*");
                        }
                        flag++;
                    }
                    buffers.append(")");
                    query.addFilterQuery(buffers.toString());
                }
                if (EmptyUtils.isNotEmpty(vo.getTradeAreaIds())) {
                    StringBuffer buffers = new StringBuffer("(");
                    int flag = 0;
                    String tradeAreaIdArray[] = vo.getTradeAreaIds().split(",");
                    for (String tradeAreaId : tradeAreaIdArray) {
                        if (flag == 0) {
                            buffers.append("tradingAreaIds : " + "," + tradeAreaId + ",");
                        } else {
                            buffers.append(" OR tradingAreaIds : " + "," + tradeAreaId + ",");
                        }
                        flag++;
                    }
                    buffers.append(")");
                    query.addFilterQuery(buffers.toString());
                }
                if (EmptyUtils.isNotEmpty(vo.getMinPrice()) && !vo.getMaxPrice().equals("1.7976931348623157e+308")) {
                        query.addFilterQuery("minPrice:" + "[ * TO " + vo.getMaxPrice() + "]");
                }
                if (EmptyUtils.isNotEmpty(vo.getMinPrice()) && !vo.getMinPrice().equals("1.7976931348623157e+308")) {
                    query.addFilterQuery("minPrice:" + "[" + vo.getMinPrice() + " TO * ]");
                }
                if (EmptyUtils.isNotEmpty(vo.getAscSort())) {
                    query.addSort(vo.getAscSort(), SolrQuery.ORDER.asc);
                }
                if (EmptyUtils.isNotEmpty(vo.getDescSort())) {
                    query.addSort(vo.getDescSort(), SolrQuery.ORDER.desc);
                }
            }
            if (EmptyUtils.isNotEmpty(tempQuery.toString())) {
                query.setQuery(tempQuery.toString());
            }
        Pages<ItripHotelVO> pages = hotelVOBaseQuery.queryPage(query, pageNo, pageSize, ItripHotelVO.class);
        return pages;
    }
    @Override
    public List<ItripHotelVO> searchItripHotelListByCityId(Integer cityId, Integer cout)throws Exception{
        SolrQuery solrQuery=new SolrQuery("*:*");
        if (EmptyUtils.isNotEmpty(cityId)){
            solrQuery.addFilterQuery("cityId:"+cityId);
        }else {
            return null;
        }
        List<ItripHotelVO> list =hotelVOBaseQuery.queryList(solrQuery,cout,ItripHotelVO.class);
        return list;
    }
}
